package com.my.servlet;

import com.delambo.drool.interfaces.*;
import com.delambo.drool.pooling.*;
import com.delambo.drool.exceptions.*;
import com.delambo.drool.utilities.*;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*; 
import java.sql.*;

public class ExampleServlet extends HttpServlet {
   
    enum Pools { TEST };
    private Drool drool;
    
    @Override public void init() { 
        try {
            /**
             * One option for the Drool factory can take three params: 
             *	log properties file - .properties file name (in class path or 
             *		relative path must be prepended)
             *	pool properties file - .properties file name (in class path or 
             *		relative path must be prepended)
             *  id - an identifier/Object, which can be used to lookup this pool
             */
            drool = DroolFactory.create("logger", "pool", Pools.TEST);
            /**
             * Alternatively, multiple pools could be constructed with multiple
             * .properties files in the two param factory method (String, HashMap):
             *  enum Pools { SLAVE, MASTER };
             *  Map poolmap = new HashMap();
             *  poolmap.add(Pools.MASTER, "/Users/delambo/webapps/myproject/classes/master_props");
             *  poolmap.add(Pools.SLAVE, "Users/delambo/webapps/myproject/classes/slave_props");
             *  drool = DroolFactory.create("path/logger", poolmap);
             */
        } catch(DroolException de) {}
    }
    
    protected void processRequest(HttpServletRequest request, 
    		HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection con = null;
        try {
            con = drool.getConnection(Pools.TEST);
            out.print("drooling: " + con);
            synchronized(this) {
                try {
                    wait(20);
                }  catch(Exception e) { e.printStackTrace(); }
            }
            /**
             * For debugging, testing, or general purposes, a print state function
             * can be invoked which will output the state of the given connection
             * pool at the given location to the log, which gives you detailed 
             * information about each node in the pool.
             */
            drool.printState(Pools.TEST, "outputting from the test servlet...");
            con.commit();
        } catch (Exception pe) { pe.printStackTrace(); } 
        finally {
            try {
            	/**
            	 * ALWAYS return connections in a finally statement, or make 
            	 * sure they get back to the pool, somehow.
            	 */
                drool.returnConnection(Pools.TEST, con);
            } catch(DroolException de) {}
            out.close();
        }
    }
    
    @Override public void destroy() {
        try {
            // If multiple pools were created, closeAllPools() can be called.
            drool.closePool(Pools.TEST);
        } catch(DroolException de) {}
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    		throws ServletException, IOException {
        processRequest(request, response);
    } 
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    		throws ServletException, IOException {
        processRequest(request, response);
    }
}
